from helpers import updateInfo
import math
import os
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    """ Email alerts notifications class """
    def get(self):
        user=users.get_current_user()
        
        # Make sure user is logged in, if not redirect to login page
        if not user:
            self.redirect(users.create_login_url(self.request.uri))
            return
        
        # Load user info from database
        # If user is using app for first time (=his info in not in TrendUsers table)
        # then initialize his info
        current_user=updateInfo.getCurrentUser(user)
        
        if current_user.alert==0.0:
            alertNum=""
        else:
            alertNum=current_user.alert*100
        
        template_values = {
            'current_user_totalamount': current_user.totalamount,
            'current_user_bankamount': current_user.bankamount,
            'current_user_investamount': current_user.totalamount-current_user.bankamount,
            'logusername' : user.nickname().capitalize(),
            'loginurl' : users.create_logout_url("/"),
            'logintext' : 'Sign Out',
            'alertNum': alertNum,
            'categoryName' : 'Request Alerts %s' % updateInfo.createToolTip("alert"),
            'setHead' : 1,
            'headContent' : 'alert_head.html',
            'maincontent' : 'account_main.html',
            'account_main_content' : 'alert_main.html'
            
        }
        path = os.path.join(os.path.dirname(__file__), updateInfo.controllersIndexPath)
        self.response.out.write(template.render(path, template_values))

    def post(self):
 
        user=users.get_current_user()
        # If user is using app for first time (=his info in not in TrendUsers table)
        # then initialize his info
        current_user=updateInfo.getCurrentUser(user)
     
        # Check how many stocks were bought
        # Buy stock selected selected    
        if (self.request.get('action')=="updateAlert"):
            try:
                changeAlert=math.fabs(float(self.request.get('alertnum')))
            except:
                self.redirect('/report?msg=14')
                return
            
            changeAlert=(changeAlert/100)
            current_user.alert=changeAlert
            current_user.put()
                        
            template_values = {
                'current_user_totalamount': current_user.totalamount,
                'current_user_bankamount': current_user.bankamount,
                'current_user_investamount': current_user.totalamount-current_user.bankamount,
                'logusername' : user.nickname().capitalize(),
                'loginurl' : users.create_logout_url("/"),
                'logintext' : 'Sign Out',
                'categoryName' : 'Alert Updated',
                'setHead' : 0,
                'maincontent' : 'account_main.html',
                'account_main_content' : 'alert_results.html'
            }
            path = os.path.join(os.path.dirname(__file__), updateInfo.controllersIndexPath)
            self.response.out.write(template.render(path, template_values))
        

            
application = webapp.WSGIApplication(
                                     [('/alert', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
